package com.kyxx.topics.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.kyxx.topics.R;
import com.kyxx.topics.activity.DetailActivity;
import com.kyxx.topics.bean.ArticleBean;
import com.kyxx.topics.bean.TopicBean;
import com.kyxx.topics.net.UrlUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;


/**
 * A simple {@link Fragment} subclass.
 */
public class BannerItemFragment extends Fragment implements OnClickListener {

    private int position;
    private ImageView mImageView;
    private int imageRes;
    private ArticleBean articleBean;
    private String imageUrl;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisk(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .showImageOnFail(R.mipmap.image_default)
            .showImageOnLoading(R.mipmap.image_default)
            .showImageForEmptyUri(R.mipmap.image_default)
            .build();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_banner_item,
                container, false);
        mImageView = (ImageView) inflate.findViewById(R.id.imageView1);
        imageLoader.displayImage(imageUrl, mImageView, options);
        inflate.setOnClickListener(this);
        return inflate;
    }

    public void setResId(int imageRes) {
        this.imageRes = imageRes;
    }

    public void setResUrl(String url) {
        if (url != null) {
            this.imageUrl = UrlUtils.APIHTTP + url;
        } else {
            this.imageUrl = "";
        }
    }

    @Override
    public void onClick(View v) {
        // TODO: 2016/8/19
        if (articleBean != null) {
            Intent intent = new Intent(getActivity(), DetailActivity.class);
            intent.putExtra("id", articleBean.getId());
            intent.putExtra("title", articleBean.getTitle());
            startActivity(intent);
        }
    }

    public void setBannerInfo(ArticleBean articleBean) {
        this.articleBean = articleBean;
    }

}
